package com.filldream.fastboot.core.manager;

import com.filldream.fastboot.base.mapper.ClsNewsMapper;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.entity.db.CfNews;
import com.filldream.fastboot.common.okutil.RobotUtil;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.common.okutil.stock.cf.EastCfUtil;
import com.filldream.fastboot.common.util.JsonUtil;
import com.filldream.fastboot.core.service.websocket.ClsNewsServer;
import com.filldream.fastboot.core.thread.ThreadPoolUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class CfNewsManager {

    @Resource
    private ClsNewsMapper clsNewsMapper;

    /**
     * 获取新闻
     * 及时获取，将没有的新闻记录在表
     * @return
     */
    public R<List<CfNews>> refreshNews() {
        List<CfNews> cfNewsList = EastCfUtil.getNews();
        List<CfNews> waitAddList = new LinkedList<>();
        if( ObjectUtils.isNotEmpty( cfNewsList ) ){
            //查询已有的数据
            List<String> codes = clsNewsMapper.getAllId(2);
            for (CfNews cfNews : cfNewsList) {
                ClsNewsSimpleItem clsNews = cfNews.toClsNewsSimpleItem();
                boolean hasFlag = codes.stream().anyMatch(t -> t.equals(cfNews.getCode()));
                if( !hasFlag && clsNewsMapper.hasRepeatTitle(cfNews.getTitle(),2) <= 0){
                    int insert = clsNewsMapper.insert(clsNews);
                    if(insert > 0){
                        waitAddList.add(cfNews);
                    }
                }
            }
        }
        //推送消息到ws
        ThreadPoolUtil.R.getExecutor().execute(() -> {
            List<ClsNewsSimpleItem> newsList = new LinkedList<>();
            waitAddList.forEach(t -> newsList.add(t.toClsNewsSimpleItem()));
            new ClsNewsServer().sendNewsList(newsList);
        });
        return R.autoSuccess(waitAddList);
    }

    /**
     * 自动发送消息给机器人
     * @return
     */
    public B autoSendOwner(List<String> senOwnerList,Boolean forceFlag){
        List<ClsNewsSimpleItem> zrWaitList = new ArrayList<>();
        List<ClsNewsSimpleItem> sunWaitList = new ArrayList<>();
        List<ClsNewsSimpleItem> addList = clsNewsMapper.getNotSendOwnerList(senOwnerList,2);
        for (ClsNewsSimpleItem item : addList) {
            List<String> sendOwnerList = item.getSendOwnerList();
            if( !sendOwnerList.contains("zr") ){
                zrWaitList.add(item.toClsNewsSimpleItem());
                sendOwnerList.add("zr");
            }
            if( !sendOwnerList.contains("sun") && (!RobotUtil.isSleepTime() || forceFlag) ){
                sunWaitList.add(item.toClsNewsSimpleItem());
                sendOwnerList.add("sun");
            }
            item.setSendOwner(JsonUtil.toJson(sendOwnerList));
            clsNewsMapper.updateById(item);
        }
        //发消息
        ThreadPoolUtil.J.getExecutor().execute( () ->{
            Map<String, String> btnMap = new HashMap<>();
            String refreshUrl = "https://finance.eastmoney.com/a/ccjdd.html";
            btnMap.put("来源:东方财富", refreshUrl);
            RobotUtil.autoDD( sunWaitList, btnMap );
            RobotUtil.autoFs( zrWaitList );
        });
        return B.success();
    }

}
